package com.ltg.framework.auth.access.handler;

import com.ltg.framework.auth.AuthInfo;
import com.ltg.framework.auth.AuthService;
import com.ltg.framework.auth.api.AuthAccessHandler;
import com.ltg.framework.auth.api.constants.Constant;
import com.ltg.framework.auth.api.jwt.JwtUtil;
import com.ltg.framework.auth.api.vo.AccessResponseVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @Version 1.0
 * @Author LTG
 * @ClassName SimpleAuthAccessHandler
 * @Date 2023/11/17 17:23
 */
public class SimpleAuthAccessHandler implements AuthAccessHandler {

    private final AuthService authService;

    public SimpleAuthAccessHandler(AuthService authService) {
        this.authService = authService;
    }


    /**
     * 获取用户信息
     *
     * @param token
     * @return
     * @throws IOException
     */
    @Override
    public AccessResponseVo handleAccess(String token) throws IOException {
        //解码
        Jws<Claims> jws = JwtUtil.parseJWT(token);
        Claims body = jws.getBody();
        String id = body.getId();
        AuthInfo authInfo = authService.getById(Long.valueOf(id));
        return AccessResponseVo.create()
                .accessToken(token)
                .authInfo(authInfo)
                .needRefresh(false)
                .build();
    }


}
